<?php
/*
  $Id$
  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com
  Copyright (c) 2010 osCommerce
  Released under the GNU General Public License
 */
class bm_categories {

    var $code = 'bm_categories';
    var $group = 'boxes';
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;
    var $category_name = '';

    function bm_categories() {
        $this->title = MODULE_BOXES_CATEGORIES_TITLE;
        $this->description = MODULE_BOXES_CATEGORIES_DESCRIPTION;

        if (defined('MODULE_BOXES_CATEGORIES_STATUS')) {
            $this->sort_order = MODULE_BOXES_CATEGORIES_SORT_ORDER;
            $this->enabled = (MODULE_BOXES_CATEGORIES_STATUS == 'True');

            $this->group = ((MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT == 'Left Column') ? 'boxes_column_left' : 'boxes_column_right');
        }
    }

    function tep_show_category($counter) {
        global $tree, $categories_string, $cPath_array;

        $selectedclass="";
        
        if(isset($cPath_array) && in_array($counter, $cPath_array))
        {
            $selectedclass = 'selected';
        }
        
        if ($tree[$counter]['level'] == '0') {
            if ($tree[$counter]['next_id'] == '' && $tree[$counter]['level'] == '0') {
                $categories_string .= '<li class="last">';
            } else {
                $categories_string .= '<li>';
            }

            if ($tree[$counter]['parent'] == 0) {
                $cPath_new = 'cPath=' . $counter;
            } else {
                $cPath_new = 'cPath=' . $tree[$counter]['path'];
            }

            $categories_string .= '<a class="menulink '.$selectedclass.'" href="' . tep_href_link(FILENAME_CATEGORY, $cPath_new) . '">';

            

            // display category name
            $categories_string .= $tree[$counter]['name'];
            /*
            if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                $categories_string .= '</strong>';
            }*/

            if (SHOW_COUNTS == 'true') {
                $products_in_category = tep_count_products_in_category($counter);
                if ($products_in_category > 0) {
                    $categories_string .= '(' . $products_in_category . ')';
                }
            }

            $categories_string .= '</a>';
            $categories_string.=$this->single_genealogy($counter);
            $categories_string.='</li>';
        }
        if($tree[$counter]['next_id'] != false) {
            $this->tep_show_category($tree[$counter]['next_id']);
        }
    }

    function getData() {
        global $categories_string, $tree, $languages_id, $cPath, $cPath_array;

        $categories_string = '';
        $tree = array();

        $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '0' and c.categories_id = cd.categories_id and cd.language_id='" . (int) $languages_id . "' order by sort_order, cd.categories_name");
        while ($categories = tep_db_fetch_array($categories_query)) {
            $tree[$categories['categories_id']] = array('name' => $categories['categories_name'],
                'parent' => $categories['parent_id'],
                'level' => 0,
                'path' => $categories['categories_id'],
                'next_id' => false);

            if (isset($parent_id)) {
                $tree[$parent_id]['next_id'] = $categories['categories_id'];
            }

            $parent_id = $categories['categories_id'];

            if (!isset($first_element)) {
                $first_element = $categories['categories_id'];
            }
        }

        if (tep_not_null($cPath)) {
            $new_path = '';
            reset($cPath_array);
            while (list($key, $value) = each($cPath_array)) {
                unset($parent_id);
                unset($first_id);
                $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int) $value . "' and c.categories_id = cd.categories_id and cd.language_id='" . (int) $languages_id . "' order by sort_order, cd.categories_name");
                if (tep_db_num_rows($categories_query)) {
                    $new_path .= $value;
                    while ($row = tep_db_fetch_array($categories_query)) {
                        $tree[$row['categories_id']] = array('name' => $row['categories_name'],
                            'parent' => $row['parent_id'],
                            'level' => $key + 1,
                            'path' => $new_path . '_' . $row['categories_id'],
                            'next_id' => false);

                        if (isset($parent_id)) {
                            $tree[$parent_id]['next_id'] = $row['categories_id'];
                        }

                        $parent_id = $row['categories_id'];

                        if (!isset($first_id)) {
                            $first_id = $row['categories_id'];
                        }

                        $last_id = $row['categories_id'];
                    }
                    $tree[$last_id]['next_id'] = $tree[$value]['next_id'];
                    $tree[$value]['next_id'] = $first_id;
                    $new_path .= '_';
                } else {
                    break;
                }
            }
        }

        if ($categoryarray == 'Y')
            $this->tep_show_category_array($first_element);
        else
            $this->tep_show_category($first_element);
        //$categories_string.=$this->single_genealogy($parent_id);
        //$this->single_genealogy($first_element);
        $data = '<ul class="menu" id="menu">' . $categories_string . '</ul>';
        return $data;
    }

    function single_genealogy($category, $level = 0) {
        global $tree, $categories_string,$cPath_array;
        // the sons of the current node = the IDs that are matched with this parentid
        $q = "SELECT c.categories_id,cd.categories_name,c.parent_id FROM categories c,categories_description cd WHERE c.parent_id='" . $category . "' AND c.categories_id = cd.categories_id ORDER by sort_order,cd.categories_name";
        $r = mysql_query($q);  //or  die/mail/echo  mysql_error()
        $level++;
        

        if (mysql_num_rows($r) != 0) {
            $categories_string .= "<ul>";
            while ($d = mysql_fetch_row($r)) {
                $cPath_new = 'cPath=' . $tree[$d[0]]['path'];
                $selectedclass="";
                if(isset($cPath_array) && in_array($d[0],$cPath_array))
                {
                    $selectedclass = 'selected';
                }
                $categories_string .= '<li><a class="menu '.$selectedclass.'" href="' . tep_href_link(FILENAME_CATEGORY, $cPath_new) . '">' . $d[1] . '(' . tep_count_products_in_category($d[0]) . ')</a>';
                //displaying  current  node  :  $d[1]
                //recursive  call  :
                $categories_string .=$this->single_genealogy($d[0], $level);
                $categories_string .="</li>";
            }
            $categories_string .= "</ul>";
        }
    }

    function get_left_menu($category_id) {
        global $tree;
        $this->getData();
        $this->category_name = $tree[$category_id]['name'];
        $left_menu_string = "";
        foreach ($tree as $key => $categorarray) 
        {
            if ($categorarray['parent'] == $category_id) {
                $left_menu_string.='<li>';
                $cPath_new = 'cPath=' . $categorarray['path'];
                $left_menu_string .= '<a href="' . tep_href_link(FILENAME_CATEGORY, $cPath_new) . '">';
                $left_menu_string .= $categorarray['name'];
                $left_menu_string .='</a></li>';
            }
        }
        return $left_menu_string;
    }

    function execute() {
        global $SID, $oscTemplate;

        if ((USE_CACHE == 'true') && empty($SID)) {
            $output = tep_cache_categories_box();
        } else {
            $output = $this->getData();
        }
        $oscTemplate->addBlock($output, $this->group);
    }

    function isEnabled() {
        return $this->enabled;
    }

    function check() {
        return defined('MODULE_BOXES_CATEGORIES_STATUS');
    }

    function install() {
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Categories Module', 'MODULE_BOXES_CATEGORIES_STATUS', 'True', 'Do you want to add the module to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Placement', 'MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT', 'Left Column', 'Should the module be loaded in the left or right column?', '6', '1', 'tep_cfg_select_option(array(\'Left Column\', \'Right Column\'), ', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_BOXES_CATEGORIES_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
    }

    function remove() {
        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
        return array('MODULE_BOXES_CATEGORIES_STATUS', 'MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT', 'MODULE_BOXES_CATEGORIES_SORT_ORDER');
    }
}
?>